/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2014 by Generalplus Inc.                         *
 *                                                                        *
 *  This software is copyrighted by and is the property of Generalplus    *
 *  Inc. All rights are reserved by Generalplus Inc.                      *
 *  This software may only be used in accordance with the                 *
 *  corresponding license agreement. Any unauthorized use, duplication,   *
 *  distribution, or disclosure of this software is expressly forbidden.  *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Generalplus Technology Co., Ltd.                   *
 *                                                                        *
 *  Generalplus Inc. reserves the right to modify this software           *
 *  without notice.                                                       *
 *                                                                        *
 *  Generalplus Inc.                                                      *
 *  No.19, Industry E. Rd. IV, Hsinchu Science Park                       *
 *  Hsinchu City 30078, Taiwan, R.O.C.                                    *
 *                                                                        *
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "drv_l1_sfr.h"
#include "drv_l1_mipi.h"
#include "drv_l1_i2c.h"
#include "drv_l2_sensor.h"
#include "drv_l2_sccb.h"
#include "drv_l2_cdsp.h"
#include "drv_l2_cdsp_config.h"
#include "gplib_print_string.h"

#if (defined _SENSOR_GC1603_CDSP_MIPI) && (_SENSOR_GC1603_CDSP_MIPI == 1)

#include "drv_l2_user_calibration.h"

/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/
#ifndef DISABLE
#define DISABLE 0
#endif
#ifndef ENABLE
#define ENABLE	1
#endif

#define MIPI_ISR_TEST				0
#define MIPI_LANE_NO				MIPI_1_LANE // 1 or 2 lane
#if (MIPI_LANE_NO == MIPI_2_LANE)
#define MIPI_DEV_NO 				0				//2Lane only selcet MIPI_0
#else
#define MIPI_DEV_NO					0				//1Lane can 0:MIPI_0 or 1:MIPI_1
#endif

#define	GC1603_ID					0x52

/*MIPI Rx Input & ISP Input*/
#define GC1603_WIDTH				1280
#define GC1603_HEIGHT				720

/*ISP Output*/
#define GC1603_OUT_WIDTH			1280
#define GC1603_OUT_HEIGHT			720


#define GC1603_25FPS_50HZ_DAY_EV_IDX		140
#define GC1603_25FPS_50HZ_NIGHT_EV_IDX		200
#define GC1603_25FPS_50HZ_EXP_TIME_TOTAL	(323)
#define GC1603_25FPS_50HZ_INIT_EV_IDX		((GC1603_25FPS_50HZ_DAY_EV_IDX + GC1603_25FPS_50HZ_NIGHT_EV_IDX) >> 1)
#define GC1603_25FPS_50HZ_MAX_EV_IDX		(GC1603_25FPS_50HZ_EXP_TIME_TOTAL-1)

#define GC1603_25FPS_60HZ_DAY_EV_IDX		140
#define GC1603_25FPS_60HZ_NIGHT_EV_IDX		200
#define GC1603_25FPS_60HZ_EXP_TIME_TOTAL	(320)
#define GC1603_25FPS_60HZ_INIT_EV_IDX		((GC1603_25FPS_60HZ_DAY_EV_IDX + GC1603_25FPS_60HZ_NIGHT_EV_IDX) >> 1)
#define GC1603_25FPS_60HZ_MAX_EV_IDX		(GC1603_25FPS_60HZ_EXP_TIME_TOTAL-1)


/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/

static int						*p_expTime_table;
static sensor_exposure_t		seInfo;
static int						pre_sensor_a_gain, pre_sensor_time;

#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW

sccb_config_t	gc1603_handle =
{
	SENSOR_I2C_SCL,		//scl_port
	SENSOR_I2C_DRIVING,	//scl_drv
	SENSOR_I2C_SDA,		//sda_port
	SENSOR_I2C_DRIVING,	//sda_drv
	0,					//pwdn_port
	0,					//pwdn_drv
	0,					//have_pwdn
	16,					//RegBits
	8,					//DataBits
	GC1603_ID,			//slaveAddr
	0x20000,			//timeout
	200					//clock_rate
};

#elif SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW

static drv_l1_i2c_bus_handle_t	gc1603_handle =
{
	SENSOR_I2C_DEV,
	GC1603_ID,
	100
};
#endif

static mipi_config_t gc1603_mipi_cfg =
{
	DISABLE,			/* low power, 0:disable, 1:enable */

#if MIPI_LANE_NO == MIPI_1_LANE

	D_PHY_SAMPLE_POS,	/* byte clock edge, 0:posedge, 1:negedge */
	MIPI_1_LANE,		/* lane */
	D_PHY_BYTE_CLK,		/* byte clock source */
#elif MIPI_LANE_NO == MIPI_2_LANE
	D_PHY_SAMPLE_POS,
	MIPI_2_LANE,		/* lane */
	GENERATE_PIXEL_CLK, /* byte clock source */
#endif
	MIPI_AUTO_DETECT,	/* data mode, 0:auto detect, 1:user define */
	MIPI_RAW10,			/* data type, valid when data mode is 1*/
	MIPI_DATA_TO_CDSP,	/* data type, 1:data[7:0]+2':00 to cdsp, 0: 2'00+data[7:0] to csi */
	0,					//NULL,			/* RSD 2 */
	GC1603_WIDTH,		/* width, 0~0xFFFF */
	GC1603_HEIGHT,		/* height, 0~0xFFFF */
#if SENSOR_FLIP
	0,
	4,
#else
	0,					/* back porch, 0~0xF *///If Set 4 have show paper color at left side.u2.20160119
	4,
#endif
	ENABLE,				/* blanking_line_en, 0:disable, 1:enable */
	0,					//NULL,			/* RSD 3 */
	ENABLE,				/* ecc, 0:disable, 1:enable */
	MIPI_ECC_ORDER3,	/* ecc order */

	250,/* data mask, unit is ns */ /*250 for 48Mhz, 1400 for 40Mhz*/

	MIPI_CHECK_HS_SEQ	//MIPI_CHECK_LP_00	/* check hs sequence or LP00 for clock lane */
};

static const regval16_t	gc1603_init_table[] =
{
	/*SYSTEM*/
	{0x031c,0x01}, //[4]2pclk_enable [3]dvpclk_enable
	{0x0317,0x24}, //[0]apwd
	{0x0320,0x77}, //[4]plldg_ldo_en [0]pllmp_ldo_en
	{0x0106,0x78}, //[6]fifo_rst_mode
	{0x0324,0x04}, //[7]div2_sys [6]plldg_en [4:0]rpllclk_div
	{0x0327,0x03}, //[2:0]refdg_div
	{0x0325,0x00}, //[2:0]dg_prediv
	{0x0326,0x20}, //25[7:0]plldg_div //fuhan 22
	{0x031a,0x00}, //[0]wrpllclk_div //Clock Manager-dgw
	{0x0314,0x30}, //[5:4]rclk_sel [3:0]rclk_div N+1 //Clock Manager-dgr
	{0x0315,0x23}, //Chargepump
	/*MIPI*/   
	{0x0334,0x40}, //[6]pllmp_en 
	{0x0337,0x03}, //[2:0]refmp_div
	{0x0335,0x01}, //[3:2]scaler_mode [1:0]pllmp_prediv
	{0x0336,0x3b}, //[7:0]pllmp_div //fuhan 4lane 23 //2lane 46
	/*PLL_EN*/ 
	{0x0324,0xc4}, //[7]div2_sys [6]plldg_en [4:0]rpllclk_div
	{0x0334,0x40}, //[6]pllmp_en
	/*RELOCK*/ 
	{0x031c,0x03}, //[4]2pclk_enable [3]dvpclk_enable
	/*CLK_EN*/ 
	{0x031c,0xd2}, //[4]2pclk_enable [3]dvpclk_enable
	{0x0180,0x26}, //[6]mipi en mp 
	{0x031c,0xd6}, //[4]2pclk_enable [3]dvpclk_enable
	/*CISCTL*/ 
	{0x0287,0x18}, //[6]AEC_delay
	{0x02ee,0x70}, //[6:4]dwen
	{0x0202,0x02}, //exp_H
	{0x0203,0xa6}, //exp_L
	{0x0213,0x1c}, //[7:0]vs_st
	{0x0214,0x04}, //[7:0]vs_et
	{0x0290,0x00}, //frame legnth depend on FL
	{0x029d,0x08}, //min_vb
	{0x0340,0x03},
	{0x0341,0x20}, //FL
	/*30fps*/  
	{0x0342,0x03}, //HB_H
	{0x0343,0x20}, //HB_L
	/*voltage-vref*/
	{0x023c,0x06},
	{0x02d1,0xe2},
	/*voltage-rowsghigh*/
	{0x027d,0xcc},
	{0x0238,0xa4}, //[2]ramp_hr_en 2.7v
	/*voltage-rsthigh*/
	{0x02ce,0x1f}, //2.8v
	/*voltage-txhigh*/
	{0x02f9,0x00}, //pad
	/*voltage-txlow*/
	{0x0227,0x74}, //[6:4]txl_drv_mode[1]CISCTL_bypass
	{0x0232,0xc8},
	/*voltage-rselhigh*/
	{0x0245,0xa8}, //[7]colen
	{0x027d,0xcc},
	/*voltage-dcghigh*/
	{0x02fa,0xb0},
	/*main,0xmode*/
	{0x02e7,0x23}, //[7:4]set_gflag_mode[3:0]rst_gflag_mode//row_dcg=88
	{0x02e8,0x50}, //change0925//41[6]is_coldcg_high_gain[5]rampr_eq_mode[0]ramp_cmp_en
	/*row & pxd*/
	{0x021d,0x13}, //double rst
	{0x0220,0x43}, //[7]orbl
	{0x0228,0x10}, //add0925//remove rstg rstdcg
	{0x022c,0x2c}, //add0925//rsel always on
	{0x024b,0x08}, //change0925//11//tx1_rest_width
	{0x024e,0x11}, //change0925//00
	{0x024d,0x11}, //change0925//00
	{0x0255,0x11}, //tx1_read_width
	{0x025b,0x11}, //multi_reset_width
	{0x0262,0x01}, //[7:6]gating mode [5:4]clear mode [3:0]restg mode
	{0x02d4,0x10}, //[7:4]rst_dcg_mode [3:0]CISCTL_ramps_offset[11:8]
	{0x0540,0x10},
	{0x0239,0x00},
	/*ramp*/   
	{0x0231,0xc4}, //[3]high_resolution_en
	{0x024f,0x11}, //ramp_t1_width
	{0x028c,0x1a}, //ramp_t1_ref
	{0x02d3,0x01}, //change0925//05//[3:1]prsel[0]vpump_en
	{0x02da,0x35}, //0a//[5:4]dcntinf_mode[3:0]ramps_width[11:8]
	{0x02db,0xd0}, //[7:0]ramps_width 1280x2
	{0x02e6,0x30}, //ramp offset
	{0x0512,0x00}, //add0925
	{0x0513,0x00}, //add0925
	{0x0515,0x1c}, //change0925//08//dacin_offset
	{0x0518,0x00}, //05//ramp_cmp_X1
	{0x0519,0x00}, //02//ramp_cmp_X2
	{0x051d,0x50}, //51//[0]ramp_cmp_mode4_en
	/*col*/    
	{0x0211,0x00}, //00[7]rampr_count[6]ramps_count[5:4]stspd2_mode[3] CISCTL_switch_read_sram[2]CISCTL_switch_write_sram[1]ckeo_switch[0] CISCTL_dark_15fps_mode2
	{0x0216,0x00}, //[7]bypass_rampr_data[6]bypass_ramps_data[5]fix_rampr_data[4] fix_ramps_data[3:2]c2clpen_mode[1:0]c1clpen_mode 
	{0x0221,0x50}, //change0925//29//eqc2
	{0x0223,0xcc}, //change0925//3b//[7:4]gpsw_width
	{0x0225,0x07}, //07[7:6]stspd_mode[5]pwd_rc[4]isp_quiet_gate[3]analog_signal_out_mode [2]rst_rc[1:0]txl_s_mode
	{0x0229,0x36}, //change0925//29//eqc1
	{0x022b,0x0c}, //add0925//fix EQC
	{0x022e,0x0c}, //change0925//33//[7:4]nbds_width[3:0]c1nb1_width
	{0x0230,0x03}, //add0925//fix EQC
	{0x023a,0x38}, //add0925//colisel
	{0x027b,0x3c}, //change0925//33//[3:0]ramp_v12_s_width
	{0x027c,0x0c}, //add0925//eqc2sel eqc1sel
	{0x0298,0x13}, //[3:2]init_pxd_end[1:0] [1:0]c1clpen_end[1:0]
	{0x02a4,0x07}, //init_pxd_end[9:2]
	{0x02ab,0x00}, //add0925//fix EQC
	{0x02ac,0x00}, //add0925//fix EQC
	{0x02ad,0x07}, //c1clpen_end[9:2]
	{0x02af,0x01}, //add0925//change with freq
	{0x02cd,0x3c}, //change0925//33//[7:4]calramp_vref_width[3:0]c2clp_width
	{0x02d2,0xe8}, //add0925//c2cas c2clamp
	{0x02e4,0x00}, //change0925//04//[5:4]ctdsun_mode[3:0]EQ_NBD_s_sw_pp
	{0x0530,0x04}, //change0925//change with freq
	{0x0531,0x04}, //change0925//change with freq
	{0x0243,0x36},
	/*sram*/   
	{0x0219,0x07}, //[3:0]AD_pipe_number
	{0x02e5,0x28},
	{0x0338,0xaa},
	{0x0339,0xaa},
	{0x033a,0x02},
	/*counter*/
	{0x023b,0x20}, //[4]vosrampen
	/*width&height*/
	{0x0212,0x48}, //[5]dummy_add_mode
	{0x0523,0x02}, //dummy_row_base
	{0x0347,0x06},
	{0x0348,0x0a},
	{0x0349,0x10}, //cisctl_win_width
	{0x034a,0x05},
	{0x034b,0xb0}, //cisctl_win_height
	{0x034c,0x05},
	{0x034d,0x00}, //out_win_width
	{0x034e,0x02},
	{0x034f,0xd0}, //out_win_height
	{0x0354,0x01},
#if SENSOR_FLIP
	{0x0101,0x03},
#else
    {0x0101,0x00},
#endif	
	/*DARK_mode*/
	{0x0295,0xff},//0x0229
	{0x0296,0x50}, //0x0221
	{0x02f0,0x22}, //0x024b
	{0x02f1,0x11}, //0x024d
	{0x02f2,0x11}, //0x024e
	{0x02f4,0x19}, //0x02f3
	{0x02f5,0x08}, //0x0235
	{0x02f6,0x1c}, //{0x02a4, 0x0298[3:2]}[7:0]
	{0x02f7,0x1f}, //{0x02a4, 0x0298[1:0]}
	{0x02f8,0x00}, //{0x02a4[7:6], 0x02ad[7:6]}
	{0x0291,0x04}, //0x0244 notuse
	{0x0292,0x11}, //0x024f
	{0x0297,0x1a}, //0x028c
	{0x02d5,0xfe}, //0x02d9
	{0x02d6,0xd0}, //0x02d6
	{0x02d7,0x35}, //0x02d7
	/*1603 Mode Read Once*/
	{0x021f,0x10}, //[7]multi_mode[1:0]multi_time 
	{0x0233,0x01}, //fullsize [3:0]hsync_multi_en
	{0x0234,0x03}, //[7]quartic_is_once_mode [1:0]row_binning
	{0x0224,0x01},
	/*CISCTL rst*/
	{0x031c,0x80},
	{0x031f,0x10},
	{0x031f,0x00},
	{0x031c,0xd2},
	{0x031c,0xd2},
	{0x031c,0xd2},
	{0x031c,0xd2},
	{0x031c,0x80},
	{0x031f,0x10},
	{0x031f,0x00},
	{0x031c,0xd6},
	/*CFA*/    
	{0x0053,0x00}, //[3:2]active_CFA[1:0]dark_CFA
	/*GAIN*/   
	{0x0205,0xc0}, //[7]PGA_col_code_disable
	{0x02b0,0xe0}, //change0925//d8
	{0x02b1,0xe0}, //change0925//d8
	{0x02b3,0x00}, //change0925//d8
	{0x02b4,0x00}, //change0925//d8
	/*BLK*/    
	{0x0226,0x30},
	{0x0042,0x20},
	{0x0458,0x00},
	{0x0459,0x00},
	{0x045a,0x00},
	{0x045b,0x00},
	{0x044c,0x5a},
	{0x044d,0x5a},
	{0x044e,0x5a},
	{0x044f,0x5a},
	{0x0060,0x10}, //wb_offset
	/*SOC_DPC*/
	{0x0080,0x04}, //[4]soc_en
	{0x00a0,0x15}, //[4]soc_size_auto_mode[3]soc_auto_mode[0]soc_dpc_bayer_en
	{0x00c7,0x90}, //soc_sca_mode[7]4M[4]auto_row_time
	{0x00c8,0x05},
	{0x00e1,0x88}, //[5:4]noise_value[3:0]dd_mode 
	{0x00e2,0x1c},
	{0x00e4,0x01},
	{0x00e5,0x01},
	{0x00e6,0x01}, //dpc_bayer_a
	{0x00e7,0x00},
	{0x00e8,0x00},
	{0x00e9,0x00}, //dpc_bayer_b
	{0x00ea,0xf0}, //dd2_th_h
	{0x00ef,0x04}, //[3:2]01-fullsize dpc
	/*OUT*/    
	{0x0089,0x03},
	{0x008c,0x10},
	{0x0080,0x04},
	/*mipi*/   
	{0x0180,0x66}, //[6]mipi en mp 
	{0x0181,0x10}, //f8//[7:4]data_en
	{0x0182,0x55},
	{0x0185,0x01}, //01//[0]dphy_clk_en
	{0x0114,0x00}, //[1:0]lane
	{0x0115,0x12}, //[1:0]clk_mode
	{0x0103,0x00},
	{0x0104,0x20}, // fifo1_prog_full
	{0x0100,0x09}, //[3]lane_en[0]mipi_en digital
	//write Done
	{0xffff,0xff}
};

static const int GC1603_mipi_25fps_exp_time_gain_50Hz[GC1603_25FPS_50HZ_EXP_TIME_TOTAL][3] =
{
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)}, //20
{8 ,(int)(1.00*64), (int)(1.00*64)},
{8 ,(int)(1.00*64), (int)(1.00*64)},
{8 ,(int)(1.00*64), (int)(1.00*64)},
{8 ,(int)(1.00*64), (int)(1.00*64)},
{9 ,(int)(1.00*64), (int)(1.00*64)},
{9 ,(int)(1.00*64), (int)(1.00*64)},
{9 ,(int)(1.00*64), (int)(1.00*64)},
{10 ,(int)(1.00*64), (int)(1.00*64)},
{10 ,(int)(1.00*64), (int)(1.00*64)},
{10 ,(int)(1.00*64), (int)(1.00*64)},
{11 ,(int)(1.00*64), (int)(1.00*64)},
{11 ,(int)(1.00*64), (int)(1.00*64)},
{11 ,(int)(1.00*64), (int)(1.00*64)},
{12 ,(int)(1.00*64), (int)(1.00*64)},
{12 ,(int)(1.00*64), (int)(1.00*64)},//35
{13 ,(int)(1.00*64), (int)(1.00*64)},
{13 ,(int)(1.00*64), (int)(1.00*64)},
{14 ,(int)(1.00*64), (int)(1.00*64)},
{14 ,(int)(1.00*64), (int)(1.00*64)},
{15 ,(int)(1.00*64), (int)(1.00*64)},
{15 ,(int)(1.00*64), (int)(1.00*64)},
{16 ,(int)(1.00*64), (int)(1.00*64)},
{16 ,(int)(1.00*64), (int)(1.00*64)},
{17 ,(int)(1.00*64), (int)(1.00*64)},
{17 ,(int)(1.00*64), (int)(1.00*64)},
{18 ,(int)(1.00*64), (int)(1.00*64)},
{19 ,(int)(1.00*64), (int)(1.00*64)},
{19 ,(int)(1.00*64), (int)(1.00*64)},
{20 ,(int)(1.00*64), (int)(1.00*64)},
{21 ,(int)(1.00*64), (int)(1.00*64)},
{21 ,(int)(1.00*64), (int)(1.00*64)},
{22 ,(int)(1.00*64), (int)(1.00*64)},
{23 ,(int)(1.00*64), (int)(1.00*64)},
{24 ,(int)(1.00*64), (int)(1.00*64)},
{24 ,(int)(1.00*64), (int)(1.00*64)},
{25 ,(int)(1.00*64), (int)(1.00*64)},
{26 ,(int)(1.00*64), (int)(1.00*64)},
{27 ,(int)(1.00*64), (int)(1.00*64)},
{28 ,(int)(1.00*64), (int)(1.00*64)},
{29 ,(int)(1.00*64), (int)(1.00*64)},
{30 ,(int)(1.00*64), (int)(1.00*64)},
{31 ,(int)(1.00*64), (int)(1.00*64)},
{32 ,(int)(1.00*64), (int)(1.00*64)},
{32 ,(int)(1.00*64), (int)(1.00*64)},
{33 ,(int)(1.00*64), (int)(1.00*64)},
{34 ,(int)(1.00*64), (int)(1.00*64)},
{35 ,(int)(1.00*64), (int)(1.00*64)},
{36 ,(int)(1.00*64), (int)(1.00*64)},
{37 ,(int)(1.00*64), (int)(1.00*64)},
{38 ,(int)(1.00*64), (int)(1.00*64)},
{39 ,(int)(1.00*64), (int)(1.00*64)},
{40 ,(int)(1.00*64), (int)(1.00*64)},
{41 ,(int)(1.00*64), (int)(1.00*64)},
{42 ,(int)(1.00*64), (int)(1.00*64)},
{43 ,(int)(1.00*64), (int)(1.00*64)},
{44 ,(int)(1.00*64), (int)(1.00*64)},
{45 ,(int)(1.00*64), (int)(1.00*64)},
{46 ,(int)(1.00*64), (int)(1.00*64)},
{47 ,(int)(1.00*64), (int)(1.00*64)},
{48 ,(int)(1.00*64), (int)(1.00*64)},
{49 ,(int)(1.00*64), (int)(1.00*64)},
{50 ,(int)(1.00*64), (int)(1.00*64)},
{52 ,(int)(1.00*64), (int)(1.00*64)},
{54 ,(int)(1.00*64), (int)(1.00*64)},
{56 ,(int)(1.00*64), (int)(1.00*64)},
{58 ,(int)(1.00*64), (int)(1.00*64)},
{60 ,(int)(1.00*64), (int)(1.00*64)},
{62 ,(int)(1.00*64), (int)(1.00*64)},
{64 ,(int)(1.00*64), (int)(1.00*64)},
{66 ,(int)(1.00*64), (int)(1.00*64)},
{68 ,(int)(1.00*64), (int)(1.00*64)},
{70 ,(int)(1.00*64), (int)(1.00*64)},
{72 ,(int)(1.00*64), (int)(1.00*64)},
{74 ,(int)(1.00*64), (int)(1.00*64)},
{75 ,(int)(1.00*64), (int)(1.00*64)},
{78 ,(int)(1.00*64), (int)(1.00*64)},
{80 ,(int)(1.00*64), (int)(1.00*64)},
{83 ,(int)(1.00*64), (int)(1.00*64)},
{85 ,(int)(1.00*64), (int)(1.00*64)},
{88 ,(int)(1.00*64), (int)(1.00*64)},
{90 ,(int)(1.00*64), (int)(1.00*64)},
{93 ,(int)(1.00*64), (int)(1.00*64)},
{95 ,(int)(1.00*64), (int)(1.00*64)},
{98 ,(int)(1.00*64), (int)(1.00*64)},
{100 ,(int)(1.00*64), (int)(1.00*64)},
{105 ,(int)(1.00*64), (int)(1.00*64)},
{110 ,(int)(1.00*64), (int)(1.00*64)},
{115 ,(int)(1.00*64), (int)(1.00*64)},

{120 ,(int)(1.00*64), (int)(1.00*64)},
{125 ,(int)(1.00*64), (int)(1.00*64)},
{130 ,(int)(1.00*64), (int)(1.00*64)},
{135 ,(int)(1.00*64), (int)(1.00*64)},
{140 ,(int)(1.00*64), (int)(1.00*64)},
{145 ,(int)(1.00*64), (int)(1.00*64)},
{150 ,(int)(1.00*64), (int)(1.00*64)},
{160 ,(int)(1.00*64), (int)(1.00*64)},
{170 ,(int)(1.00*64), (int)(1.00*64)},
{180 ,(int)(1.00*64), (int)(1.00*64)},
{190 ,(int)(1.00*64), (int)(1.00*64)},
{200 ,(int)(1.00*64), (int)(1.00*64)},//131
{200 ,(int)(1.04*64), (int)(1.00*64)},
{200 ,(int)(1.07*64), (int)(1.00*64)},
{200 ,(int)(1.11*64), (int)(1.00*64)},
{200 ,(int)(1.15*64), (int)(1.00*64)},
{200 ,(int)(1.19*64), (int)(1.00*64)},
{200 ,(int)(1.23*64), (int)(1.00*64)},
{200 ,(int)(1.27*64), (int)(1.00*64)},
{200 ,(int)(1.32*64), (int)(1.00*64)},
{200 ,(int)(1.37*64), (int)(1.00*64)},
{200 ,(int)(1.41*64), (int)(1.00*64)},
{200 ,(int)(1.46*64), (int)(1.00*64)},
{200 ,(int)(1.52*64), (int)(1.00*64)},
{200 ,(int)(1.57*64), (int)(1.00*64)},
{200 ,(int)(1.62*64), (int)(1.00*64)},
{200 ,(int)(1.68*64), (int)(1.00*64)},
{200 ,(int)(1.74*64), (int)(1.00*64)},
{200 ,(int)(1.80*64), (int)(1.00*64)},
{200 ,(int)(1.87*64), (int)(1.00*64)},
{200 ,(int)(1.93*64), (int)(1.00*64)},
{400 ,(int)(1.00*64), (int)(1.00*64)},//151
{400 ,(int)(1.04*64), (int)(1.00*64)},
{400 ,(int)(1.07*64), (int)(1.00*64)},
{400 ,(int)(1.11*64), (int)(1.00*64)},
{400 ,(int)(1.15*64), (int)(1.00*64)},
{400 ,(int)(1.19*64), (int)(1.00*64)},
{400 ,(int)(1.23*64), (int)(1.00*64)},
{400 ,(int)(1.27*64), (int)(1.00*64)},
{400 ,(int)(1.32*64), (int)(1.00*64)},
{400 ,(int)(1.37*64), (int)(1.00*64)},
{400 ,(int)(1.41*64), (int)(1.00*64)},
{400 ,(int)(1.46*64), (int)(1.00*64)},
{400 ,(int)(1.52*64), (int)(1.00*64)},
{400 ,(int)(1.57*64), (int)(1.00*64)},
{400 ,(int)(1.62*64), (int)(1.00*64)},
{400 ,(int)(1.68*64), (int)(1.00*64)},
{400 ,(int)(1.74*64), (int)(1.00*64)},
{400 ,(int)(1.80*64), (int)(1.00*64)},
{400 ,(int)(1.87*64), (int)(1.00*64)},
{400 ,(int)(1.93*64), (int)(1.00*64)},
{600 ,(int)(1.32*64), (int)(1.00*64)},//171
{600 ,(int)(1.37*64), (int)(1.00*64)},
{600 ,(int)(1.41*64), (int)(1.00*64)},
{600 ,(int)(1.46*64), (int)(1.00*64)},
{600 ,(int)(1.52*64), (int)(1.00*64)},
{600 ,(int)(1.57*64), (int)(1.00*64)},
{600 ,(int)(1.62*64), (int)(1.00*64)},
{600 ,(int)(1.68*64), (int)(1.00*64)},
{600 ,(int)(1.74*64), (int)(1.00*64)},
{600 ,(int)(1.80*64), (int)(1.00*64)},
{600 ,(int)(1.87*64), (int)(1.00*64)},
{600 ,(int)(1.93*64), (int)(1.00*64)},
{800 ,(int)(1.46*64), (int)(1.00*64)},//183
{800 ,(int)(1.52*64), (int)(1.00*64)},
{800 ,(int)(1.57*64), (int)(1.00*64)},
{800 ,(int)(1.62*64), (int)(1.00*64)},
{800 ,(int)(1.68*64), (int)(1.00*64)},
{800 ,(int)(1.74*64), (int)(1.00*64)},
{800 ,(int)(1.80*64), (int)(1.00*64)},
{800 ,(int)(1.87*64), (int)(1.00*64)},
{800 ,(int)(1.93*64), (int)(1.00*64)},
{800 ,(int)(2.00*64), (int)(1.00*64)},
{800 ,(int)(2.07*64), (int)(1.00*64)},
{800 ,(int)(2.14*64), (int)(1.00*64)},
{800 ,(int)(2.22*64), (int)(1.00*64)},
{800 ,(int)(2.30*64), (int)(1.00*64)},
{800 ,(int)(2.38*64), (int)(1.00*64)},
{800 ,(int)(2.46*64), (int)(1.00*64)},
{800 ,(int)(2.55*64), (int)(1.00*64)},
{800 ,(int)(2.64*64), (int)(1.00*64)},
{800 ,(int)(2.73*64), (int)(1.00*64)},
{800 ,(int)(2.83*64), (int)(1.00*64)},
{800 ,(int)(2.93*64), (int)(1.00*64)},
{800 ,(int)(3.03*64), (int)(1.00*64)},
{800 ,(int)(3.14*64), (int)(1.00*64)},
{800 ,(int)(3.25*64), (int)(1.00*64)},
{800 ,(int)(3.36*64), (int)(1.00*64)},
{800 ,(int)(3.48*64), (int)(1.00*64)},
{800 ,(int)(3.61*64), (int)(1.00*64)},
{800 ,(int)(3.73*64), (int)(1.00*64)},
{800 ,(int)(3.86*64), (int)(1.00*64)},
{800 ,(int)(4.00*64), (int)(1.00*64)},
{800 ,(int)(4.07*64), (int)(1.00*64)},
{800 ,(int)(4.14*64), (int)(1.00*64)},
{800 ,(int)(4.29*64), (int)(1.00*64)},
{800 ,(int)(4.36*64), (int)(1.00*64)},
{800 ,(int)(4.44*64), (int)(1.00*64)},
{800 ,(int)(4.59*64), (int)(1.00*64)},
{800 ,(int)(4.76*64), (int)(1.00*64)},
{800 ,(int)(4.92*64), (int)(1.00*64)},
{800 ,(int)(5.10*64), (int)(1.00*64)},
{800 ,(int)(5.28*64), (int)(1.00*64)},
{800 ,(int)(5.46*64), (int)(1.00*64)},
{800 ,(int)(5.66*64), (int)(1.00*64)},
{800 ,(int)(5.86*64), (int)(1.00*64)},
{800 ,(int)(6.06*64), (int)(1.00*64)},
{800 ,(int)(6.28*64), (int)(1.00*64)},
{800 ,(int)(6.50*64), (int)(1.00*64)},
{800 ,(int)(6.73*64), (int)(1.00*64)},
{800 ,(int)(6.96*64), (int)(1.00*64)},
{800 ,(int)(7.21*64), (int)(1.00*64)},
{800 ,(int)(7.46*64), (int)(1.00*64)},
{800 ,(int)(7.73*64), (int)(1.00*64)},
{800 ,(int)(8.00*64), (int)(1.00*64)},//232
{800 ,(int)(8.28*64), (int)(1.00*64)},
{800 ,(int)(8.57*64), (int)(1.00*64)},
{800 ,(int)(8.88*64), (int)(1.00*64)},
{800 ,(int)(9.19*64), (int)(1.00*64)},
{800 ,(int)(9.51*64), (int)(1.00*64)},
{800 ,(int)(9.85*64), (int)(1.00*64)},
{800 ,(int)(10.20*64), (int)(1.00*64)},
{800 ,(int)(10.56*64), (int)(1.00*64)},
{800 ,(int)(10.93*64), (int)(1.00*64)},
{800 ,(int)(11.31*64), (int)(1.00*64)},
{800 ,(int)(11.71*64), (int)(1.00*64)},
{800 ,(int)(12.13*64), (int)(1.00*64)},
{800 ,(int)(12.55*64), (int)(1.00*64)},
{800 ,(int)(13.00*64), (int)(1.00*64)},
{800 ,(int)(13.45*64), (int)(1.00*64)},
{800 ,(int)(13.93*64), (int)(1.00*64)},
{800 ,(int)(14.42*64), (int)(1.00*64)},
{800 ,(int)(14.93*64), (int)(1.00*64)},
{800 ,(int)(15.45*64), (int)(1.00*64)},
{800 ,(int)(16.00*64), (int)(1.00*64)},
{800 ,(int)(16.56*64), (int)(1.00*64)},
{800 ,(int)(17.15*64), (int)(1.00*64)},
{800 ,(int)(17.75*64), (int)(1.00*64)},
{800 ,(int)(18.38*64), (int)(1.00*64)},
{800 ,(int)(19.03*64), (int)(1.00*64)},
{800 ,(int)(19.70*64), (int)(1.00*64)},
{800 ,(int)(20.39*64), (int)(1.00*64)},
{800 ,(int)(21.11*64), (int)(1.00*64)},
{800 ,(int)(21.86*64), (int)(1.00*64)},
{800 ,(int)(22.63*64), (int)(1.00*64)},
{800 ,(int)(23.43*64), (int)(1.00*64)},
{800 ,(int)(24.25*64), (int)(1.00*64)},
{800 ,(int)(25.11*64), (int)(1.00*64)},
{800 ,(int)(25.99*64), (int)(1.00*64)},
{800 ,(int)(26.91*64), (int)(1.00*64)},
{800 ,(int)(27.86*64), (int)(1.00*64)},
{800 ,(int)(28.84*64), (int)(1.00*64)},
{800 ,(int)(29.86*64), (int)(1.00*64)},
{800 ,(int)(30.91*64), (int)(1.00*64)},
{800 ,(int)(32.00*64), (int)(1.00*64)},//272
{800 ,(int)(33.13*64), (int)(1.00*64)},//60
{800 ,(int)(34.30*64), (int)(1.00*64)},
{800 ,(int)(35.51*64), (int)(1.00*64)},//58
{800 ,(int)(36.76*64), (int)(1.00*64)},
{800 ,(int)(38.05*64), (int)(1.00*64)},
{800 ,(int)(39.40*64), (int)(1.00*64)},
{800 ,(int)(40.79*64), (int)(1.00*64)},
{800 ,(int)(42.22*64), (int)(1.00*64)},
{800 ,(int)(43.71*64), (int)(1.00*64)},
{800 ,(int)(45.25*64), (int)(1.00*64)},
{800 ,(int)(46.85*64), (int)(1.00*64)},
{800 ,(int)(48.50*64), (int)(1.00*64)},
{800 ,(int)(50.21*64), (int)(1.00*64)},
{800 ,(int)(51.98*64), (int)(1.00*64)},
{800 ,(int)(53.82*64), (int)(1.00*64)},
{800 ,(int)(55.72*64), (int)(1.00*64)},
{800 ,(int)(57.68*64), (int)(1.00*64)},
{800 ,(int)(59.71*64), (int)(1.00*64)},
{800 ,(int)(61.82*64), (int)(1.00*64)},
{800 ,(int)(64.00*64), (int)(1.00*64)},//292
{800 ,(int)(66.26*64), (int)(1.00*64)},
{800 ,(int)(68.59*64), (int)(1.00*64)},
{800 ,(int)(71.01*64), (int)(1.00*64)},
{800 ,(int)(73.52*64), (int)(1.00*64)},
{800 ,(int)(76.11*64), (int)(1.00*64)},
{800 ,(int)(78.79*64), (int)(1.00*64)},
{800 ,(int)(81.57*64), (int)(1.00*64)},
{800 ,(int)(84.45*64), (int)(1.00*64)},
{800 ,(int)(87.43*64), (int)(1.00*64)},
{800 ,(int)(90.51*64), (int)(1.00*64)},
{800 ,(int)(93.70*64), (int)(1.00*64)},
{800 ,(int)(97.01*64), (int)(1.00*64)},
{800 ,(int)(100.43*64), (int)(1.00*64)},
{800 ,(int)(103.97*64), (int)(1.00*64)},
{800 ,(int)(107.63*64), (int)(1.00*64)},
{800 ,(int)(111.43*64), (int)(1.00*64)},
{800 ,(int)(115.36*64), (int)(1.00*64)},
{800 ,(int)(119.43*64), (int)(1.00*64)},
{800 ,(int)(123.64*64), (int)(1.00*64)},
{800 ,(int)(128.00*64), (int)(1.00*64)},//312
{800 ,(int)(132.51*64), (int)(1.00*64)},
{800 ,(int)(137.19*64), (int)(1.00*64)},
{800 ,(int)(142.02*64), (int)(1.00*64)},
{800 ,(int)(147.03*64), (int)(1.00*64)},
{800 ,(int)(152.22*64), (int)(1.00*64)},
{800 ,(int)(157.59*64), (int)(1.00*64)},
{800 ,(int)(163.14*64), (int)(1.00*64)},
{800 ,(int)(168.90*64), (int)(1.00*64)},
{800 ,(int)(174.85*64), (int)(1.00*64)},
{800 ,(int)(181.02*64), (int)(1.00*64)},
{800 ,(int)(187.40*64), (int)(1.00*64)},
{800 ,(int)(194.01*64), (int)(1.00*64)},
{800 ,(int)(200.85*64), (int)(1.00*64)},
{800 ,(int)(207.94*64), (int)(1.00*64)},
{800 ,(int)(215.27*64), (int)(1.00*64)},
{800 ,(int)(222.86*64), (int)(1.00*64)},
{800 ,(int)(230.72*64), (int)(1.00*64)},
{800 ,(int)(238.86*64), (int)(1.00*64)},
{800 ,(int)(247.28*64), (int)(1.00*64)},
{800 ,(int)(256.00*64), (int)(1.00*64)},//332

};

static const int GC1603_mipi_25fps_exp_time_gain_60Hz[GC1603_25FPS_60HZ_EXP_TIME_TOTAL][3] =
{						// {time, analog gain, digital gain}
{3 ,(int)(1.00*64), (int)(1.00*64)},
{3 ,(int)(1.00*64), (int)(1.00*64)},
{3 ,(int)(1.00*64), (int)(1.00*64)},
{3 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{4 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{5 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)}, //20
{6 ,(int)(1.00*64), (int)(1.00*64)},
{6 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)},
{7 ,(int)(1.00*64), (int)(1.00*64)},
{8 ,(int)(1.00*64), (int)(1.00*64)},
{8 ,(int)(1.00*64), (int)(1.00*64)},
{8 ,(int)(1.00*64), (int)(1.00*64)},
{9 ,(int)(1.00*64), (int)(1.00*64)},
{9 ,(int)(1.00*64), (int)(1.00*64)},
{9 ,(int)(1.00*64), (int)(1.00*64)},
{10   ,(int)(1.00*64), (int)(1.00*64)},
{10   ,(int)(1.00*64), (int)(1.00*64)},
{10   ,(int)(1.00*64), (int)(1.00*64)},//35
{11   ,(int)(1.00*64), (int)(1.00*64)},
{11   ,(int)(1.00*64), (int)(1.00*64)},
{11   ,(int)(1.00*64), (int)(1.00*64)},
{12   ,(int)(1.00*64), (int)(1.00*64)},
{12   ,(int)(1.00*64), (int)(1.00*64)},
{13   ,(int)(1.00*64), (int)(1.00*64)},
{13   ,(int)(1.00*64), (int)(1.00*64)},
{13   ,(int)(1.00*64), (int)(1.00*64)},
{14   ,(int)(1.00*64), (int)(1.00*64)},
{14   ,(int)(1.00*64), (int)(1.00*64)},
{15   ,(int)(1.00*64), (int)(1.00*64)},
{15   ,(int)(1.00*64), (int)(1.00*64)},
{16   ,(int)(1.00*64), (int)(1.00*64)},
{17   ,(int)(1.00*64), (int)(1.00*64)},
{17   ,(int)(1.00*64), (int)(1.00*64)},
{18   ,(int)(1.00*64), (int)(1.00*64)},
{18   ,(int)(1.00*64), (int)(1.00*64)},
{19   ,(int)(1.00*64), (int)(1.00*64)},
{20   ,(int)(1.00*64), (int)(1.00*64)},
{20   ,(int)(1.00*64), (int)(1.00*64)},
{21   ,(int)(1.00*64), (int)(1.00*64)},
{22   ,(int)(1.00*64), (int)(1.00*64)},
{23   ,(int)(1.00*64), (int)(1.00*64)},
{23   ,(int)(1.00*64), (int)(1.00*64)},
{24   ,(int)(1.00*64), (int)(1.00*64)},
{25   ,(int)(1.00*64), (int)(1.00*64)},
{26   ,(int)(1.00*64), (int)(1.00*64)},
{27   ,(int)(1.00*64), (int)(1.00*64)},
{28   ,(int)(1.00*64), (int)(1.00*64)},
{29   ,(int)(1.00*64), (int)(1.00*64)},
{30   ,(int)(1.00*64), (int)(1.00*64)},
{31   ,(int)(1.00*64), (int)(1.00*64)},
{32   ,(int)(1.00*64), (int)(1.00*64)},
{33   ,(int)(1.00*64), (int)(1.00*64)},
{34   ,(int)(1.00*64), (int)(1.00*64)},
{36   ,(int)(1.00*64), (int)(1.00*64)},
{37   ,(int)(1.00*64), (int)(1.00*64)},
{38   ,(int)(1.00*64), (int)(1.00*64)},
{39   ,(int)(1.00*64), (int)(1.00*64)},
{41   ,(int)(1.00*64), (int)(1.00*64)},
{42   ,(int)(1.00*64), (int)(1.00*64)},
{44   ,(int)(1.00*64), (int)(1.00*64)},
{45   ,(int)(1.00*64), (int)(1.00*64)},
{47   ,(int)(1.00*64), (int)(1.00*64)},
{48   ,(int)(1.00*64), (int)(1.00*64)},
{50   ,(int)(1.00*64), (int)(1.00*64)},
{52   ,(int)(1.00*64), (int)(1.00*64)},
{54   ,(int)(1.00*64), (int)(1.00*64)},
{56   ,(int)(1.00*64), (int)(1.00*64)},
{58   ,(int)(1.00*64), (int)(1.00*64)},
{60   ,(int)(1.00*64), (int)(1.00*64)},
{62   ,(int)(1.00*64), (int)(1.00*64)},
{64   ,(int)(1.00*64), (int)(1.00*64)},
{65   ,(int)(1.00*64), (int)(1.00*64)},
{67   ,(int)(1.00*64), (int)(1.00*64)},
{69   ,(int)(1.00*64), (int)(1.00*64)},
{70   ,(int)(1.00*64), (int)(1.00*64)},
{72   ,(int)(1.00*64), (int)(1.00*64)},
{74   ,(int)(1.00*64), (int)(1.00*64)},
{76   ,(int)(1.00*64), (int)(1.00*64)},
{78  ,(int)(1.00*64), (int)(1.00*64)},
{80  ,(int)(1.00*64), (int)(1.00*64)},
{82  ,(int)(1.00*64), (int)(1.00*64)},
{84  ,(int)(1.00*64), (int)(1.00*64)},
{86  ,(int)(1.00*64), (int)(1.00*64)},
{88 ,(int)(1.00*64), (int)(1.00*64)},
{90 ,(int)(1.00*64), (int)(1.00*64)},
{92 ,(int)(1.00*64), (int)(1.00*64)},
{95 ,(int)(1.00*64), (int)(1.00*64)},
{98 ,(int)(1.00*64), (int)(1.00*64)},
{100 ,(int)(1.00*64), (int)(1.00*64)},
{105 ,(int)(1.00*64), (int)(1.00*64)},
{110 ,(int)(1.00*64), (int)(1.00*64)},
{115 ,(int)(1.00*64), (int)(1.00*64)},
{120 ,(int)(1.00*64), (int)(1.00*64)},

{125 ,(int)(1.00*64), (int)(1.00*64)},
{130 ,(int)(1.00*64), (int)(1.00*64)},
{135 ,(int)(1.00*64), (int)(1.00*64)},
{140 ,(int)(1.00*64), (int)(1.00*64)},
{145 ,(int)(1.00*64), (int)(1.00*64)},
{150 ,(int)(1.00*64), (int)(1.00*64)},
{155 ,(int)(1.00*64), (int)(1.00*64)},
{160 ,(int)(1.00*64), (int)(1.00*64)},
{166,(int)(1.00*64), (int)(1.00*64)},//131
{166,(int)(1.04*64), (int)(1.00*64)},
{166,(int)(1.07*64), (int)(1.00*64)},
{166,(int)(1.11*64), (int)(1.00*64)},
{166,(int)(1.15*64), (int)(1.00*64)},
{166,(int)(1.19*64), (int)(1.00*64)},
{166,(int)(1.23*64), (int)(1.00*64)},
{166,(int)(1.27*64), (int)(1.00*64)},
{166,(int)(1.32*64), (int)(1.00*64)},
{166,(int)(1.37*64), (int)(1.00*64)},
{166,(int)(1.41*64), (int)(1.00*64)},
{166,(int)(1.46*64), (int)(1.00*64)},
{166,(int)(1.52*64), (int)(1.00*64)},
{166,(int)(1.57*64), (int)(1.00*64)},
{166,(int)(1.62*64), (int)(1.00*64)},
{166,(int)(1.68*64), (int)(1.00*64)},
{166,(int)(1.74*64), (int)(1.00*64)},
{166,(int)(1.80*64), (int)(1.00*64)},
{166,(int)(1.87*64), (int)(1.00*64)},
{166,(int)(1.93*64), (int)(1.00*64)},
{332,(int)(1.00*64), (int)(1.00*64)},//151
{332,(int)(1.04*64), (int)(1.00*64)},
{332,(int)(1.07*64), (int)(1.00*64)},
{332,(int)(1.11*64), (int)(1.00*64)},
{332,(int)(1.15*64), (int)(1.00*64)},
{332,(int)(1.19*64), (int)(1.00*64)},
{332,(int)(1.23*64), (int)(1.00*64)},
{332,(int)(1.27*64), (int)(1.00*64)},
{332,(int)(1.32*64), (int)(1.00*64)},
{332,(int)(1.37*64), (int)(1.00*64)},
{332,(int)(1.41*64), (int)(1.00*64)},
{332,(int)(1.46*64), (int)(1.00*64)},
{332,(int)(1.52*64), (int)(1.00*64)},
{332,(int)(1.57*64), (int)(1.00*64)},
{332,(int)(1.62*64), (int)(1.00*64)},
{332,(int)(1.68*64), (int)(1.00*64)},
{332,(int)(1.74*64), (int)(1.00*64)},
{332,(int)(1.80*64), (int)(1.00*64)},
{332,(int)(1.87*64), (int)(1.00*64)},
{332,(int)(1.93*64), (int)(1.00*64)},
{498 ,(int)(1.32*64), (int)(1.00*64)},//171
{498 ,(int)(1.37*64), (int)(1.00*64)},
{498 ,(int)(1.41*64), (int)(1.00*64)},
{498 ,(int)(1.46*64), (int)(1.00*64)},
{498 ,(int)(1.52*64), (int)(1.00*64)},
{498 ,(int)(1.57*64), (int)(1.00*64)},
{498 ,(int)(1.62*64), (int)(1.00*64)},
{498 ,(int)(1.68*64), (int)(1.00*64)},
{498 ,(int)(1.74*64), (int)(1.00*64)},
{498 ,(int)(1.80*64), (int)(1.00*64)},
{498 ,(int)(1.87*64), (int)(1.00*64)},
{498 ,(int)(1.93*64), (int)(1.00*64)},
{664 ,(int)(1.46*64), (int)(1.00*64)},//183
{664 ,(int)(1.52*64), (int)(1.00*64)},
{664 ,(int)(1.57*64), (int)(1.00*64)},
{664 ,(int)(1.62*64), (int)(1.00*64)},
{664 ,(int)(1.68*64), (int)(1.00*64)},
{664 ,(int)(1.74*64), (int)(1.00*64)},
{664 ,(int)(1.80*64), (int)(1.00*64)},
{664 ,(int)(1.87*64), (int)(1.00*64)},
{664 ,(int)(1.93*64), (int)(1.00*64)},
{664 ,(int)(2.00*64), (int)(1.00*64)},
{664 ,(int)(2.07*64), (int)(1.00*64)},
{664 ,(int)(2.14*64), (int)(1.00*64)},
{664 ,(int)(2.22*64), (int)(1.00*64)},
{664 ,(int)(2.30*64), (int)(1.00*64)},
{664 ,(int)(2.38*64), (int)(1.00*64)},
{664 ,(int)(2.46*64), (int)(1.00*64)},
{664 ,(int)(2.55*64), (int)(1.00*64)},
{664 ,(int)(2.64*64), (int)(1.00*64)},
{664 ,(int)(2.73*64), (int)(1.00*64)},
{664 ,(int)(2.83*64), (int)(1.00*64)},
{664 ,(int)(2.93*64), (int)(1.00*64)},
{664 ,(int)(3.03*64), (int)(1.00*64)},
{664 ,(int)(3.14*64), (int)(1.00*64)},
{664 ,(int)(3.25*64), (int)(1.00*64)},
{664 ,(int)(3.36*64), (int)(1.00*64)},
{664 ,(int)(3.48*64), (int)(1.00*64)},
{664 ,(int)(3.61*64), (int)(1.00*64)},
{664 ,(int)(3.73*64), (int)(1.00*64)},
{664 ,(int)(3.86*64), (int)(1.00*64)},
{664 ,(int)(4.00*64), (int)(1.00*64)},
{664 ,(int)(4.14*64), (int)(1.00*64)},
{664 ,(int)(4.29*64), (int)(1.00*64)},
{664 ,(int)(4.44*64), (int)(1.00*64)},
{664 ,(int)(4.59*64), (int)(1.00*64)},
{664 ,(int)(4.76*64), (int)(1.00*64)},
{664 ,(int)(4.92*64), (int)(1.00*64)},
{664 ,(int)(5.10*64), (int)(1.00*64)},
{664 ,(int)(5.28*64), (int)(1.00*64)},
{664 ,(int)(5.46*64), (int)(1.00*64)},
{664 ,(int)(5.66*64), (int)(1.00*64)},
{664 ,(int)(5.86*64), (int)(1.00*64)},
{664 ,(int)(6.06*64), (int)(1.00*64)},
{664 ,(int)(6.28*64), (int)(1.00*64)},
{664 ,(int)(6.50*64), (int)(1.00*64)},
{664 ,(int)(6.73*64), (int)(1.00*64)},
{664 ,(int)(6.96*64), (int)(1.00*64)},
{664 ,(int)(7.21*64), (int)(1.00*64)},
{664 ,(int)(7.46*64), (int)(1.00*64)},
{664 ,(int)(7.73*64), (int)(1.00*64)},
{664 ,(int)(8.00*64), (int)(1.00*64)},//232
{664 ,(int)(8.28*64), (int)(1.00*64)},
{664 ,(int)(8.57*64), (int)(1.00*64)},
{664 ,(int)(8.88*64), (int)(1.00*64)},
{664 ,(int)(9.19*64), (int)(1.00*64)},
{664 ,(int)(9.51*64), (int)(1.00*64)},
{664 ,(int)(9.85*64), (int)(1.00*64)},
{664 ,(int)(10.20*64), (int)(1.00*64)},
{664 ,(int)(10.56*64), (int)(1.00*64)},
{664 ,(int)(10.93*64), (int)(1.00*64)},
{664 ,(int)(11.31*64), (int)(1.00*64)},
{664 ,(int)(11.71*64), (int)(1.00*64)},
{664 ,(int)(12.13*64), (int)(1.00*64)},
{664 ,(int)(12.55*64), (int)(1.00*64)},
{664 ,(int)(13.00*64), (int)(1.00*64)},
{664 ,(int)(13.45*64), (int)(1.00*64)},
{664 ,(int)(13.93*64), (int)(1.00*64)},
{664 ,(int)(14.42*64), (int)(1.00*64)},
{664 ,(int)(14.93*64), (int)(1.00*64)},
{664 ,(int)(15.45*64), (int)(1.00*64)},
{664 ,(int)(16.00*64), (int)(1.00*64)},
{664 ,(int)(16.56*64), (int)(1.00*64)},
{664 ,(int)(17.15*64), (int)(1.00*64)},
{664 ,(int)(17.75*64), (int)(1.00*64)},
{664 ,(int)(18.38*64), (int)(1.00*64)},
{664 ,(int)(19.03*64), (int)(1.00*64)},
{664 ,(int)(19.70*64), (int)(1.00*64)},
{664 ,(int)(20.39*64), (int)(1.00*64)},
{664 ,(int)(21.11*64), (int)(1.00*64)},
{664 ,(int)(21.86*64), (int)(1.00*64)},
{664 ,(int)(22.63*64), (int)(1.00*64)},
{664 ,(int)(23.43*64), (int)(1.00*64)},
{664 ,(int)(24.25*64), (int)(1.00*64)},
{664 ,(int)(25.11*64), (int)(1.00*64)},
{664 ,(int)(25.99*64), (int)(1.00*64)},
{664 ,(int)(26.91*64), (int)(1.00*64)},
{664 ,(int)(27.86*64), (int)(1.00*64)},
{664 ,(int)(28.84*64), (int)(1.00*64)},
{664 ,(int)(29.86*64), (int)(1.00*64)},
{664 ,(int)(30.91*64), (int)(1.00*64)},
{664 ,(int)(32.00*64), (int)(1.00*64)},//272
{664 ,(int)(33.13*64), (int)(1.00*64)},
{664 ,(int)(34.30*64), (int)(1.00*64)},
{664 ,(int)(35.51*64), (int)(1.00*64)},
{664 ,(int)(36.76*64), (int)(1.00*64)},
{664 ,(int)(38.05*64), (int)(1.00*64)},
{664 ,(int)(39.40*64), (int)(1.00*64)},
{664 ,(int)(40.79*64), (int)(1.00*64)},
{664 ,(int)(42.22*64), (int)(1.00*64)},
{664 ,(int)(43.71*64), (int)(1.00*64)},
{664 ,(int)(45.25*64), (int)(1.00*64)},
{664 ,(int)(46.85*64), (int)(1.00*64)},
{664 ,(int)(48.50*64), (int)(1.00*64)},
{664 ,(int)(50.21*64), (int)(1.00*64)},
{664 ,(int)(51.98*64), (int)(1.00*64)},
{664 ,(int)(53.82*64), (int)(1.00*64)},
{664 ,(int)(55.72*64), (int)(1.00*64)},
{664 ,(int)(57.68*64), (int)(1.00*64)},
{664 ,(int)(59.71*64), (int)(1.00*64)},
{664 ,(int)(61.82*64), (int)(1.00*64)},
{664 ,(int)(64.00*64), (int)(1.00*64)},//292
{664 ,(int)(66.26*64), (int)(1.00*64)},
{664 ,(int)(68.59*64), (int)(1.00*64)},
{664 ,(int)(71.01*64), (int)(1.00*64)},
{664 ,(int)(73.52*64), (int)(1.00*64)},
{664 ,(int)(76.11*64), (int)(1.00*64)},
{664 ,(int)(78.79*64), (int)(1.00*64)},
{664 ,(int)(81.57*64), (int)(1.00*64)},
{664 ,(int)(84.45*64), (int)(1.00*64)},
{664 ,(int)(87.43*64), (int)(1.00*64)},
{664 ,(int)(90.51*64), (int)(1.00*64)},
{664 ,(int)(93.70*64), (int)(1.00*64)},
{664 ,(int)(97.01*64), (int)(1.00*64)},
{664 ,(int)(100.43*64), (int)(1.00*64)},
{664 ,(int)(103.97*64), (int)(1.00*64)},
{664 ,(int)(107.63*64), (int)(1.00*64)},
{664 ,(int)(111.43*64), (int)(1.00*64)},
{664 ,(int)(115.36*64), (int)(1.00*64)},
{664 ,(int)(119.43*64), (int)(1.00*64)},
{664 ,(int)(123.64*64), (int)(1.00*64)},
{664 ,(int)(128.00*64), (int)(1.00*64)},//312
{664 ,(int)(132.51*64), (int)(1.00*64)},
{664 ,(int)(137.19*64), (int)(1.00*64)},
{664 ,(int)(142.02*64), (int)(1.00*64)},
{664 ,(int)(147.03*64), (int)(1.00*64)},
{664 ,(int)(152.22*64), (int)(1.00*64)},
{664 ,(int)(157.59*64), (int)(1.00*64)},
{664 ,(int)(163.14*64), (int)(1.00*64)},
{664 ,(int)(168.90*64), (int)(1.00*64)},
{664 ,(int)(174.85*64), (int)(1.00*64)},
{664 ,(int)(181.02*64), (int)(1.00*64)},
{664 ,(int)(187.40*64), (int)(1.00*64)},
{664 ,(int)(194.01*64), (int)(1.00*64)},
{664 ,(int)(200.85*64), (int)(1.00*64)},
{664 ,(int)(207.94*64), (int)(1.00*64)},
{664 ,(int)(215.27*64), (int)(1.00*64)},
{664 ,(int)(222.86*64), (int)(1.00*64)},
{664 ,(int)(230.72*64), (int)(1.00*64)},
{664 ,(int)(238.86*64), (int)(1.00*64)},
{664 ,(int)(247.28*64), (int)(1.00*64)},
{664 ,(int)(256.00*64), (int)(1.00*64)}//332

};

////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTION IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////////////////////////////
static INT32S gc1603_sccb_open(void)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	drv_l1_i2c_init(&gc1603_handle);
	DBG_PRINT("SENSOR_I2C_MODE_HW\r\n");
#else
	DBG_PRINT("SENSOR_I2C_MODE_SW\r\n");
#endif

	return STATUS_OK;
}

static void gc1603_sccb_close(void)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	drv_l1_i2c_uninit(&gc1603_handle);
#endif
}


static INT32S gc1603_sccb_write(INT16U reg, INT8U value)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
	return drv_l2_sccb_write(&gc1603_handle, reg, value);
#else
	return drv_l1_reg_2byte_data_1byte_write(&gc1603_handle, reg, value);
#endif
}


static INT32S gc1603_sccb_read(INT16U reg, INT8U *value)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
	INT16U data;
	if(drv_l2_sccb_read(&gc1603_handle, reg, &data) >= 0)
	{
		//DBG_PRINT("[SW] R:0x%02x, D:0x%02x\r\n", reg, data);
		*value = data;
		return STATUS_OK;
	}
	else
	{
		DBG_PRINT("i2C read fail!\n");
		return STATUS_FAIL;
	}
#else //SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW

	if(drv_l1_reg_2byte_data_1byte_read(&gc1603_handle, reg, value) >=0)
	{
		//DBG_PRINT("0x%02x, 0x%02x\r\n", reg, value);
		return STATUS_OK;
	}
	else
	{
		DBG_PRINT("gc1603_sccb_read fail...\r\n");
		return STATUS_FAIL;
	}
	
#endif	
}



static INT32S gc1603_sccb_write_table(regval16_t *pTable)
{

	INT32U retry_cnt = 0;

	while(1)
	{
		drv_l1_sys_wdt_clear();
		if(pTable->reg_num == 0xFFFF && pTable->value == 0xFF)
		{
			break;
		}

		//DBG_PRINT("0x%02x, 0x%02x\r\n", pTable->reg_num, pTable->value);
		if(gc1603_sccb_write(pTable->reg_num, pTable->value) < 0)
		{
			DBG_PRINT("sccb write fail.\r\n");
			retry_cnt += 1;
			if(retry_cnt >= 10)
				return STATUS_FAIL;
			continue;
		}
		else
		{
			retry_cnt = 0;
		}

		//gc1603_sccb_read(pTable->reg_num);

		pTable++;
	}

	return STATUS_OK;
}


static INT32S gc1603_sccb_read_table(regval16_t *pTable)
{
	
	INT8U 	Rd_data;
	while(1) {
		if(pTable->reg_num == 0xFFFF && pTable->value == 0xFF) {
			break;
		}

		if(gc1603_sccb_read(pTable->reg_num,&Rd_data) < 0) {
			DBG_PRINT("sccb read fail.\r\n");
			continue;
		}
		DBG_PRINT("addr=0x%x w_data = 0x%x r_data=0x%x \r\n",pTable->reg_num,pTable->value,Rd_data);
		pTable++;
	}
	DBG_PRINT("\r\n\r\n");

	return STATUS_OK;
}

/*****************************************************************************************
+++++++++++++++++AEC/AGC
*****************************************************************************************/


INT8U RegValTable[32][5] = 
{ 
	{0x00, 0x00, 0x01, 0x00, 0x20}, 
	{0x08, 0x00, 0x01, 0x0A, 0x20}, 
	{0x01, 0x00, 0x01, 0x19, 0x1E}, 
	{0x09, 0x00, 0x01, 0x26, 0x1C}, 
	{0x10, 0x00, 0x01, 0x3F, 0x1A}, 
	{0x18, 0x00, 0x02, 0x13, 0x18}, 
	{0x11, 0x00, 0x02, 0x31, 0x18}, 
	{0x19, 0x00, 0x03, 0x0B, 0x16}, 
	{0x30, 0x00, 0x04, 0x04, 0x16}, 
	{0x38, 0x00, 0x04, 0x2C, 0x14}, 
	{0x31, 0x00, 0x05, 0x29, 0x13}, 
	{0x39, 0x00, 0x06, 0x1F, 0x12}, 
	{0x32, 0x00, 0x07, 0x38, 0x12}, 
	{0x3a, 0x00, 0x09, 0x05, 0x12}, 
	{0x33, 0x00, 0x0B, 0x12, 0x10}, 
	{0x3b, 0x00, 0x0D, 0x00, 0x10}, 
	{0x34, 0x00, 0x10, 0x03, 0x0e}, 
	{0x3c, 0x00, 0x12, 0x1E, 0x0c}, 
	{0xb4, 0x00, 0x16, 0x00, 0x0a}, 
	{0xbc, 0x00, 0x19, 0x15, 0x08}, 
	{0x34, 0x01, 0x1F, 0x06, 0x06}, 
	{0x3c, 0x01, 0x23, 0x33, 0x04}, 
	{0xb4, 0x01, 0x2C, 0x22, 0x02}, 
	{0xbc, 0x01, 0x33, 0x12, 0x02}, 
	{0x34, 0x02, 0x3F, 0x10, 0x02}, 
	{0x3c, 0x02, 0x48, 0x34, 0x02}, 
	{0xf4, 0x01, 0x5F, 0x06, 0x02}, 
	{0xfc, 0x01, 0x6D, 0x1E, 0x02}, 
	{0x74, 0x02, 0x87, 0x00, 0x02}, 
	{0x7c, 0x02, 0x9B, 0x19, 0x02}, 
	{0x75, 0x02, 0xC7, 0x07, 0x02}, 
	{0x7d, 0x02, 0xE5, 0x0B, 0x02}, 
};


INT32U GainLevelTable[33] = 
{
 64,
 74,
 89,
 102,
 127,
 147,
 177,
 203,
 260,
 300,
 361,
 415,
 504,
 581,
 722,
 832,
 1027,
 1182,
 1408,
 1621,
 1990,
 2291,
 2850,
 3282,
 4048,
 4660,
 6086,
 7006,
 8640,
 9945,
 12743,
 14667,
 0xffffffff
};

static int gc1603_cvt_agc_gain(int analog_gain)
{
	INT16U i;
	INT16U total_cnt;
	INT32U Digital_Gain = 0;
	
	total_cnt = sizeof(GainLevelTable) / sizeof(INT32U);
   // DBG_PRINT("total_cnt=%d\r\n", total_cnt);
	for(i = 0; i < total_cnt; i++)
	{
		if((GainLevelTable[i] <= analog_gain)&&(analog_gain < GainLevelTable[i+1]))
			break;
	}
	Digital_Gain = analog_gain*64/GainLevelTable[i];

	gc1603_sccb_write(0x2b3,RegValTable[i][0]);
	gc1603_sccb_write(0x2b4,RegValTable[i][1]);
    gc1603_sccb_write(0x2b8,RegValTable[i][2]);
	gc1603_sccb_write(0x2b9,RegValTable[i][3]);
	gc1603_sccb_write(0x515,RegValTable[i][4]);
	
  	gc1603_sccb_write(0x020E, Digital_Gain >> 6);
	gc1603_sccb_write(0x020F, ((Digital_Gain&0x3f)<<2));

    return STATUS_OK;
}


static int gc1603_set_xfps_exposure_time(sensor_exposure_t *si)
{
	INT16U	t1, t2;
	INT32U	idx, temp;
	INT8S	ret, iso_x = 1;

	iso_x = (drv_l2_CdspGetIso() & 0xF);
	if(iso_x <= 0)
		iso_x = 1;

	si->sensor_ev_idx += si->ae_ev_step;
	if(si->sensor_ev_idx >= si->max_ev_idx)
		si->sensor_ev_idx = si->max_ev_idx;
	if(si->sensor_ev_idx < 0)
		si->sensor_ev_idx = 0;

	idx = si->sensor_ev_idx * 3;
	si->time = p_expTime_table[idx];
	si->analog_gain = p_expTime_table[idx + 1];
	si->digital_gain = p_expTime_table[idx+2];
	//iso set
	si->analog_gain = (si->analog_gain * iso_x);

//	DBG_PRINT("EV=%d,  ", si->sensor_ev_idx);

	// exposure time
	if(si->time != pre_sensor_time)
	{
		pre_sensor_time = si->time;
		//DBG_PRINT("[%d,", pre_sensor_time);
		
		temp = si->time;

		t1 = (temp & 0xff);
		t2 = (temp >> 8) & 0x00ff;

		ret = gc1603_sccb_write(0x0203, t1);
		if(ret < 0)
			return ret;
		ret = gc1603_sccb_write(0x0202, t2);
		if(ret < 0)
		{
			DBG_PRINT("ERR: write sensor time = 0x%x!!!\r\n", temp);
			return ret;
		}
	}

	//gain
	if(si->analog_gain != pre_sensor_a_gain)
	{
		// gain
		pre_sensor_a_gain = si->analog_gain;

		temp = si->analog_gain;
		ret = gc1603_cvt_agc_gain(temp);
		if(ret < 0)
		{
			DBG_PRINT("ERR: write sensor analog agin = 0x%x!!!\r\n", temp);
			return ret;
		}
	}

	return 0;
}


static void gc1603_set_ae(int ev_step)
{
	seInfo.ae_ev_step = ev_step;
	gc1603_set_xfps_exposure_time(&seInfo);
}

void sensor_register_ae_ctrl(INT32U *handle)
{
	*handle = (INT32U) gc1603_set_ae;
}

void sensor_get_ae_info(sensor_exposure_t *si)
{
	gp_memcpy((INT8S*)si, (INT8S*)&seInfo, sizeof(sensor_exposure_t));
}

void sensor_set_max_lum(int max_lum)
{
	seInfo.max_ev_idx = seInfo.total_ev_idx - (128 - max_lum);
	DBG_PRINT("MaxLum:%d, MaxEvIdx:%d, TotalEvIdx:%d\r\n",max_lum, seInfo.max_ev_idx, seInfo.total_ev_idx);
}


void gc1603_mipi_set_exp_freq(INT8U freq)
{
	if(freq == 50)
	{
		seInfo.sensor_ev_idx = GC1603_25FPS_50HZ_INIT_EV_IDX;
		seInfo.ae_ev_step = 0;
		seInfo.daylight_ev_idx = GC1603_25FPS_50HZ_DAY_EV_IDX;
		seInfo.night_ev_idx = GC1603_25FPS_50HZ_NIGHT_EV_IDX;
		seInfo.max_ev_idx = GC1603_25FPS_50HZ_MAX_EV_IDX;
		seInfo.total_ev_idx = GC1603_25FPS_50HZ_EXP_TIME_TOTAL;

		p_expTime_table = (int *) GC1603_mipi_25fps_exp_time_gain_50Hz;
		DBG_PRINT(">>Set frequence 50Hz.\r\n");
	}
	else
	if(freq == 60)
	{
		seInfo.sensor_ev_idx = GC1603_25FPS_60HZ_INIT_EV_IDX;
		seInfo.ae_ev_step = 0;
		seInfo.daylight_ev_idx = GC1603_25FPS_60HZ_DAY_EV_IDX;
		seInfo.night_ev_idx = GC1603_25FPS_60HZ_NIGHT_EV_IDX;
		seInfo.max_ev_idx = GC1603_25FPS_60HZ_MAX_EV_IDX;
		seInfo.total_ev_idx = GC1603_25FPS_60HZ_EXP_TIME_TOTAL;

		p_expTime_table = (int *) GC1603_mipi_25fps_exp_time_gain_60Hz;

		DBG_PRINT(">>Set frequence 60Hz.\r\n");
	}
	sensor_set_max_lum(g_FavTable.max_lum);
}

void gc1603_mipi_seinfo_init(void)
{
	seInfo.sensor_ev_idx = GC1603_25FPS_50HZ_INIT_EV_IDX;
	seInfo.ae_ev_step = 0;
	seInfo.daylight_ev_idx = GC1603_25FPS_50HZ_DAY_EV_IDX;
	seInfo.night_ev_idx = GC1603_25FPS_50HZ_NIGHT_EV_IDX;
	seInfo.max_ev_idx = GC1603_25FPS_50HZ_MAX_EV_IDX;
	seInfo.total_ev_idx = GC1603_25FPS_50HZ_EXP_TIME_TOTAL;

	p_expTime_table = (int *) GC1603_mipi_25fps_exp_time_gain_50Hz;

	pre_sensor_time = 1;
	pre_sensor_a_gain = 0x00;

	seInfo.time = 1;
	seInfo.analog_gain = 0x00;
	seInfo.digital_gain = 0x20;
}



#if MIPI_ISR_TEST
static void mipi_gc1603_handle(INT32U dev_no, INT32U event)
{
	if(event & MIPI_LANE0_SOT_SYNC_ERR)
	{
		DBG_PRINT("LANE0_SOT_SYNC_ERR\r\n");
	}

	if(event & MIPI_HD_1BIT_ERR)
	{
		DBG_PRINT("HD_1BIT_ERR\r\n");
	}

	if(event & MIPI_HD_NBIT_ERR)
	{
		DBG_PRINT("HD_NBIT_ERR\r\n");
	}

	if(event & MIPI_DATA_CRC_ERR)
	{
		DBG_PRINT("DATA_CRC_ERR\r\n");
	}

	if(event & MIPI_LANE1_SOT_SYNC_ERR)
	{
		DBG_PRINT("LANE1_SOT_SYNC_ERR\r\n");
	}

	if(event & MIPI_CCIR_SOF)
	{
		DBG_PRINT("CCIR_SOF\r\n");
	}
}

#endif

static void gc1603_mipi_sensor_init(void)
{
	// reguest sccb
	gc1603_sccb_open();

	OSTimeDly(20);

	// init sensor
	gc1603_sccb_write_table((regval16_t *) gc1603_init_table);
 //   OSTimeDly(20);
//	gc1603_sccb_read_table((regval16_t *)gc1603_init_table);

	DBG_PRINT("Sensor GC1603 mipi init completed\r\n");
}

/**
 * @brief   initialization function
 * @param   sensor format parameters
 * @return 	none
 */


void gc1603_cdsp_mipi_init(void)
{
	DBG_PRINT("%s\r\n", __func__);

	//ae init
	gc1603_mipi_seinfo_init();	/*ae init*/
	//cdsp init
	drv_l2_cdsp_open();			/*cdsp init*/

	// Reset
#ifdef FRONT_SENSOR_RESET		/*reset sensor*/
	gpio_set_port_attribute(FRONT_SENSOR_RESET, ATTRIBUTE_HIGH);
	gpio_init_io(FRONT_SENSOR_RESET, GPIO_OUTPUT);
	gpio_write_io(FRONT_SENSOR_RESET, DATA_HIGH);

	OSTimeDly(5);
	gpio_write_io(FRONT_SENSOR_RESET, DATA_LOW);

	OSTimeDly(10);
	gpio_write_io(FRONT_SENSOR_RESET, DATA_HIGH);

	OSTimeDly(10);
#endif

	// mipi enable
#if (MIPI_DEV_NO == 1)
	drv_l1_mipi_init(MIPI_1, ENABLE);
#else
	drv_l1_mipi_init(MIPI_0, ENABLE);
#endif
	DBG_PRINT("Sensor GC1603 cdsp mipi[%d] init completed\r\n", MIPI_DEV_NO);
}

/**
 * @brief   un-initialization function
 * @param   sensor format parameters
 * @return 	none
 */
static void gc1603_cdsp_mipi_uninit(void)
{
	DBG_PRINT("%s\r\n", __func__);

	// disable mclk
	drv_l2_sensor_set_mclkout(MCLK_NONE, MASTER_CLOCK_MIPI_OFF, MASTER_CLOCK_CSI_OFF);

	// cdsp disable
	drv_l2_cdsp_close();
	// mipi disable
#if (MIPI_DEV_NO == 1)
	drv_l1_mipi_init(MIPI_1, DISABLE);
#else
	drv_l1_mipi_init(MIPI_0, DISABLE);
#endif

	// release sccb
	gc1603_sccb_close();
}

/**
 * @brief   stream start function
 * @param   info index
 *
 * @return 	none
 */
void gc1603_cdsp_mipi_stream_on(INT32U index, INT32U bufA, INT32U bufB)
{
	INT16U		target_w, target_h, sensor_w, sensor_h;
	gpCdspFmt_t format;

	// set sensor size
	DBG_PRINT("%s = %d\r\n", __func__, index);

	/*Enable Mclk out*/
	//drv_l2_sensor_set_mclkout(gc1603_cdsp_mipi_ops.info[index].mclk, MASTER_CLOCK_MIPI_ON, MASTER_CLOCK_CSI_KEEP);
	drv_l1_csi_set_clock(gc1603_cdsp_mipi_ops.info[index].mclk, MASTER_CLOCK_MIPI_ON, MASTER_CLOCK_CSI_KEEP);

	//Enabel mipi clk, set mipi clk
	#if 0
	//drv_l2_mipi_ctrl_set_clk(ENABLE, 4);	//for GENERATE_PIXEL_CLK
	OSTimeDly(3);						//wait 3ms
	#endif

	/* cdsp clock */
#if MIPI_DEV_NO == 1
	drv_l1_CdspSetClockTree(ENABLE, MIPI1_INPUT, RAW);
#else
	drv_l1_CdspSetClockTree(ENABLE, MIPI_INPUT, RAW);
#endif

    // set cdsp format
#if MIPI_DEV_NO == 0
	format.image_source = C_CDSP_MIPI;
#else
	format.image_source = C_CDSP_MIPI1;
#endif
	format.input_format = gc1603_cdsp_mipi_ops.info[index].input_format;
	format.output_format = gc1603_cdsp_mipi_ops.info[index].output_format;
	target_w = gc1603_cdsp_mipi_ops.info[index].target_w;
	target_h = gc1603_cdsp_mipi_ops.info[index].target_h;
	format.hpixel = sensor_w = gc1603_cdsp_mipi_ops.info[index].sensor_w;
	format.vline = sensor_h = gc1603_cdsp_mipi_ops.info[index].sensor_h;
	format.hoffset =gc1603_cdsp_mipi_ops.info[index].hoffset;
	format.voffset = gc1603_cdsp_mipi_ops.info[index].voffset;
	format.sensor_timing_mode = gc1603_cdsp_mipi_ops.info[index].interface_mode;
	format.sensor_hsync_mode = gc1603_cdsp_mipi_ops.info[index].hsync_active;
	format.sensor_vsync_mode = gc1603_cdsp_mipi_ops.info[index].vsync_active;

	if(drv_l2_cdsp_set_fmt(&format) < 0)
	{
		DBG_PRINT("cdsp set fmt err!!!\r\n");
	}
	// set scale down
	/*
	if((format.hpixel > target_w) || (format.vline > target_h))
	{
		drv_l2_cdsp_set_yuv_scale(target_w, target_h);
	}
   */
	// cdsp start
	drv_l2_CdspTableRegister((gpCisCali_t *) &g_cali);
	drv_l2_cdsp_stream_on(ENABLE, bufA, bufB);		//Disable AE,AWB,SetSensor Task
	drv_l1_CdspSetDataSource(C_CDSP_SDRAM);
	drv_l2_cdsp_enable((gpUserFav_t*)&g_FavTable ,sensor_w, sensor_h, target_w, target_h);
	drv_l2_cdsp_sat_contrast_reinit_thr();


	//mipi start
#if (MIPI_DEV_NO == 1)
	if(drv_l1_mipi_set_parameter(MIPI_1, &gc1603_mipi_cfg) < 0)
	{
		DBG_PRINT("MIPI1 init fail !!!\r\n");
	}

		#if MIPI_ISR_TEST
		vic_irq_enable(VIC_MIPI_B);
		drv_l1_mipi_isr_register(mipi_gc1603_handle);
		drv_l1_mipi_set_irq_enable(MIPI_1, ENABLE, MIPI_INT_ALL);
		#endif
#else
	if(drv_l1_mipi_set_parameter(MIPI_0, &gc1603_mipi_cfg) < 0)
	{
		DBG_PRINT("MIPI0 init fail !!!\r\n");
	}
	else
	{
		//DBG_PRINT("MIPI0 init completed.\r\n");
		#if MIPI_ISR_TEST
		vic_irq_enable(VIC_MIPI);
		drv_l1_mipi_isr_register(mipi_gc1603_handle);
		drv_l1_mipi_set_irq_enable(MIPI_0, ENABLE, MIPI_INT_ALL);
		#endif
	}

#endif

	/*Init Sensor*/
	gc1603_mipi_sensor_init();

	// reset sensor ev idx
	seInfo.ae_ev_step = 0;
	gc1603_set_xfps_exposure_time(&seInfo);
	DBG_PRINT("GC1603 mipi Exposure Time set!!\r\n");

	drv_l1_CdspSetDataSource(C_CDSP_MIPI);
	DBG_PRINT("GC1603 cdsp mipi init done!!\r\n");

}

/**
 * @brief   stream stop function
 * @param   none
 * @return 	none
 */
static void gc1603_cdsp_mipi_stream_off(void)
{
	//drv_l2_cdsp_stream_off();
}

/**
 * @brief   get info function
 * @param   none
 * @return 	pointer to sensor information data
 */
static drv_l2_sensor_info_t *gc1603_cdsp_mipi_get_info(INT32U index)
{
	if(index > (MAX_INFO_NUM - 1))
	{
		return NULL;
	}
	else
	{
		return (drv_l2_sensor_info_t *) &gc1603_cdsp_mipi_ops.info[index];
	}
}

/*********************************************
*	sensor ops declaration
*********************************************/
const drv_l2_sensor_ops_t gc1603_cdsp_mipi_ops =
{
	SENSOR_SOURCE_MIPI,
	SENSOR_PATH_CDSP,
	gc1603_cdsp_mipi_init,
	gc1603_cdsp_mipi_uninit,
	gc1603_cdsp_mipi_stream_on,
	gc1603_cdsp_mipi_stream_off,
	NULL,
	NULL,
	gc1603_mipi_set_exp_freq,
	gc1603_cdsp_mipi_get_info,
	{
		/* 1st info */
		{
			MASTER_CLOCK_24MHZ,		/* MCLK */
			V4L2_PIX_FMT_SRGGB10,	/* input format */
			V4L2_PIX_FMT_YUYV,		/* output format */
			25,						/* FPS in sensor */
			GC1603_OUT_WIDTH,		/* target width */
			GC1603_OUT_HEIGHT,		/* target height */
			GC1603_WIDTH,			/* sensor width */
			GC1603_HEIGHT,			/* sensor height */

			#if SENSOR_FLIP
			0,						/* sensor h offset */
			0,						/* sensor v offset */
			#else
			0,
			0,
			#endif

			MODE_CCIR_601,			/* input interface */
			MODE_NONE_INTERLACE,	/* interlace mode */
			MODE_ACTIVE_HIGH,		/* hsync pin active level */
			MODE_ACTIVE_HIGH,		/* vsync pin active level */
		}
	}
};
#endif //(defined _SENSOR_GC1603_CDSP_MIPI) && (_SENSOR_GC1603_CDSP_MIPI == 1)
